package web.servlet;

import domain.Account;
import domain.Cart;
import domain.Order;
import service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class FinalOrderServlet extends HttpServlet {

    private static final String FINAL_ORDER_FORM = "/WEB-INF/jsp/order/finalOrder.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session=req.getSession();
        Cart cart=(Cart) session.getAttribute("cart");
        //Account loginAccount = (Account) session.getAttribute("loginAccount");
        Order order=(Order)session.getAttribute("order");
        OrderService orderService=new OrderService();

        orderService.setNotRepeatOrderId(order);
        orderService.insertOrder(order);
        orderService.insertOrderStatus(order);
        orderService.insertOrderItem(order);
        session.setAttribute("order",order);
        session.setAttribute("Msg","Your order has been submitted");
        req.getRequestDispatcher(FINAL_ORDER_FORM).forward(req,resp);
        cart=null;
        session.setAttribute("cart",cart);


        req.getRequestDispatcher(FINAL_ORDER_FORM).forward(req, resp);
    }
}
